package xg2013.totoro.web;

import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import xg2013.totoro.domain.DesignProject;
import xg2013.totoro.domain.Major;
import xg2013.totoro.domain.Department;
import xg2013.totoro.domain.GraduateProject;
import xg2013.totoro.domain.PaperProject;
import xg2013.totoro.domain.ProjectToMajor;
import xg2013.totoro.domain.Tutor;
import xg2013.totoro.domain.User;
import xg2013.totoro.service.DepartmentService;
import xg2013.totoro.service.DesignProjectService;
import xg2013.totoro.service.GetPresentUser;
import xg2013.totoro.service.MajorService;
import xg2013.totoro.service.PaperProjectService;
import xg2013.totoro.service.ProjectFromService;
import xg2013.totoro.service.ProjectToMajorService;
import xg2013.totoro.service.ProjectTypeService;
import xg2013.totoro.service.SchoolService;
import xg2013.totoro.service.UserService;

@Controller
@RequestMapping("process")
public class ProcessOfSelectController {
	@Autowired
	private UserService userService;
	@Autowired
	private DesignProjectService designProjectService;
	@Autowired
	private PaperProjectService paperProjectService;
	@Autowired
	private ProjectTypeService projectTypeService;
	@Autowired
	private ProjectFromService proejctFromService;
	@Autowired
	private SchoolService schoolService;
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private MajorService majorService;
	@Autowired
	private ProjectToMajorService projectToMajorService;
	
	
	
	
	
	
	
	@RequestMapping("/listProjects.html")
	public String listProjects(Model model){
		User user = userService.get(GetPresentUser.presentUserId);
		Collection<GraduateProject> projectList = ((Tutor)user.getActor()).getGraduateProjects();
		model.addAttribute("projectList", projectList);
		
		System.out.println("111111111111111111111111111111111111111111111111111111111111111111111111111");
		return "process/projectList";	
	}
	@RequestMapping("/addProject.html")
	public String addproject(Model model){
		User user = userService.get(GetPresentUser.presentUserId);
		//Collection<GraduateProject> project = ((Tutor)user.getActor()).getGraduateProjects();
		model.addAttribute("projectTypes",projectTypeService.list());
		model.addAttribute("projectFroms", proejctFromService.list());
		model.addAttribute("schools", schoolService.list());
		model.addAttribute("graduateProject", new GraduateProject());
		
		
		
		return "process/addProject";
		
		
	}
	@RequestMapping("/alterProject.html")
	public String alterProject(Model model, Integer project_id){
		
		
		return "process/alterProject";
	}
	
	
	@RequestMapping("/deleteProject.html")
	public String deleteProject(Model model, Integer progect_id){
		DesignProject project1 = designProjectService.get(progect_id);
		designProjectService.deleteObject(project1);
		PaperProject project2 = paperProjectService.get(progect_id);
		paperProjectService.deleteObject(project2);
		return "process/projectList";
	}
	
	@RequestMapping("/submitPaperProject.html")
	public String submitProject(PaperProject paperProject ){
//		PaperProject paperProject = (PaperProject)graduateProject;
		paperProjectService.saveOrUpdate(paperProject);
		
		Major major = new Major();
		System.out.println(paperProject.getProjectType().getDescription());
		
		Collection<ProjectToMajor> projectToMajors = paperProject.getProjectToMajors();
		ProjectToMajor projectToMajor = new ProjectToMajor();
//		projectToMajor.setMajor(major);
//		major.setProjectToMajors(projectToMajors);
//		paperProject.setProjectToMajors(projectToMajors);
		
		//projectToMajor.setMajor(majors);
		projectToMajor.setGraduateProject(paperProject);
		projectToMajorService.saveOrUpdate(projectToMajor);
		
		
		
		
		return "process/projectList";
		
	}
	@RequestMapping("/submitDesignProject.html")
	public String submitDesignPproject(DesignProject designProject, Integer major_id ){
		
		
		designProjectService.saveOrUpdate(designProject);
		Collection<ProjectToMajor> projectToMajors = designProject.getProjectToMajors();
		return "process/projectList";
		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	@RequestMapping(value = "/departmentList",method = RequestMethod.GET)
	public String departmentList(Integer id,ModelMap modelMap){
		System.out.println("id是:"+id);
		Collection<Department> departmentList = departmentService.list("school", schoolService.get(id));
		System.out.println("2222222222222222222222222222222222");
		modelMap.addAttribute("departmentList",departmentList);
		System.out.println("departmentList");
		return "departmentJson";
	}
	
	@RequestMapping(value = "/majorList",method = RequestMethod.GET)
	public String majorList(Integer id,ModelMap modelMap){
		Collection<Major> majorList = majorService.list("department", departmentService.get(id));
		System.out.println("老师教研室的id==="+id);
		System.out.println("2222222222222222222222222222222255555555555555555522");
		modelMap.addAttribute("majorList", majorList);
		return "majorJson";
	
	
	
	
	
	
	
	}	
}
